<template>
  <div class="spece-info">
    <!-- title；弹窗的标题 -->
    <!-- visible：控制弹窗是否可见 -->
    <!-- before-close：弹窗关闭前的回调函数 -->
    <el-dialog
      :title="info.type === 'add' ? '新增角色' : '编辑角色'"
      :visible.sync="info.isShow"
      width="60%"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="属性名" prop="specsname">
          <el-input v-model="ruleForm.specsname"></el-input>
        </el-form-item>
        <el-form-item label="属性值">
          <div v-for="(item, index) in attrList" :key="index">
            <el-input class="attr-input" v-model="attrList[index]"></el-input>
            <el-button v-if="index === 0" type="primary" @click="add"
              >新增属性</el-button
            >
            <el-button v-else type="danger" @click="del(index)"
              >删除属性</el-button
            >
          </div>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="info.type === 'add'"
            type="primary"
            @click="submitForm"
            >立即创建</el-button
          >
          <el-button v-else type="primary" @click="submitForm"
            >立即编辑</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getMenuList, cateSpecs, roleEdit } from "../../api";
export default {
  props: ["info"],
  data() {
    return {
      // 控制表单项的数组
      attrList: [""],
      // 表单中的数据
      ruleForm: {
        specsname: "",
        attrs: "",
        // 1正常2禁用
        status: 1,
      },
      // 校验规则
      rules: {
        rolename: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 设置表单的值
    setVal(data) {
      // 深拷贝，避免数据互相影响
      this.ruleForm = { ...data };
      // 区分新增和编辑
      if(this.ruleForm.attrs) {
        this.attrList = [...this.ruleForm.attrs];
      }else {
        this.attrList = [""]
      }
    },
    // 增加表单项
    add() {
      if (this.attrList.length < 5) {
        this.attrList.push("");
      } else {
        this.$message({
          type: "warning",
          message: '最多添加5条',
        });
      }
    },
    // 删除表单项
    del(index) {
      this.attrList.splice(index, 1);
    },
    async getTreeList() {
      let res = await getMenuList();
      this.treeData = res.list;
    },
    submitForm() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.ruleForm.attrs = this.attrList.join(",");
          console.log(this.ruleForm);
          if (this.info.type === "add") {
            // 添加
            let res = await cateSpecs(this.ruleForm);
            // 关闭弹窗
            this.close();
          } else {
            // 编辑
            let res = await roleEdit(this.ruleForm);
            // 关闭弹窗
            this.close();
          }
          // 更新list
          this.$emit("update");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 关闭弹窗
    close() {
      this.info.isShow = false;
    },
  },
};
</script>

<style>
.spece-info .attr-input {
  width: 200px;
  margin-right: 20px;
  margin-bottom: 10px;
}
</style>